

<h3>Why generate code?</h3>
<p>In brief, you can generate code to create non-existent classes. As an example, look at the <a href="{{ site.mage2000url }}app/code/Magento/Customer/Model/Resource/AddressRepository.php" target="_blank">\Magento\Customer\Model\Resource\AddressRepository</a> constructor. A snippet follows:</p>

{% highlight php %}
<?php
...
    public function __construct(
        \Magento\Customer\Model\AddressFactory $addressFactory,
...
?>
{% endhighlight %}

<p>The first constructor parameter has a type of <code>\Magento\Customer\Model\AddressFactory</code>. However, this class does not exist in <code>\Magento\Customer\Model</code> in the Magento 2 GitHub. The Magento application <em>generates</em> this class as because its name uses a recognized convention (in this case, because the name ends with <code>Factory</code>).</p>
<p>Unlike some other languages or libraries, you can look at the generated code on the file system to see what really happens and still debug through the code.</p>

<h4>When is code generated?</h4>
<p>Provided the Magento application is <em>not</em> set for <a href="{{ page.baseurl }}/config-guide/bootstrap/magento-modes.html#production-mode">production mode</a>, code is generated when the Magento application cannot find a class when executing code.</p>
<p>You can also use the compiler discussed in this topic to generate code at any time.</p>

<h4>Proxies and factories</h4>
<ul><li>A <a href="{{ page.baseurl }}/extension-dev-guide/depend-inj.html#dep-inj-mod-type-fact">Factory</a> class creates instances of a type; for example, a generated <code>\Magento\Customer\Model\AddressFactory</code> creates new instances of <code>\Magento\Customer\Model\Address</code>. The code in <code>AddressFactory</code> has some specific code for the <code>Address</code> type.</li>
<li>In more complex code generation, you can designate a <a href="{{ page.baseurl }}/extension-dev-guide/depend-inj.html#dep-inj-preview-cons">Proxy</a> to be generated for a type.<br>
 Generally, a proxy must have an implementation of all the declared public methods of the original class.</br>
The method implementation could be delegating to another object in memory, or the method could make a network call to another object on a different machine. All the Proxy methods in a class usually do the same thing (for example, they all delegate to another object or they all make a network call) except they need a slight difference to call a specific method.<br>
As a practical example, you can see the <a href="{{ site.mage2000url }}app/code/Magento/Store/Model/StoreManager.php" target="_blank">StoreManager</a> class and then see the generated StoreManager Proxy class.</li></ul>

<h4>Why should you regenerate code?</h4>
<p>Suppose a Customer or Proxy class for a Customer class is generated. The Customer class has new methods added to it. Because a Customer or Proxy exists on the file system, it will not be re-generated. However, the Customer or Proxy implementation is incomplete now because it does not have the new methods. In this case, you must regenerate the Customer or Proxy class.</p>
<p>Rarely, if the code generator implementation itself is changed, you must regenerate all the classes.</p>

<h3>Advantages of generating code</h3>
<p>Generating code assures you of the following:</p>

<ul><li>The code is correct.<br>
You won’t have to worry that the generated code is delegating to the wrong method or forgetting a semicolon. You don’t have to write tests for the generated code. </li>
<li>代码生成 writes the boilerplate code to enable you to write the more challenging and interesting code.</li>
    <li>Consistency in implementation. <br>
    All generated Factories work the same way. After you learn know how one Factory works, you should know how they all work.</li>
    <li>You can change the implementation for all generated code. <br>
    If you discover a better way of implementing a Proxy, you can do it across the board. If you want the code generator to use a PHP <code>__call</code> magic method or if you want to real methods, you only need to change the generator. Code maintenance is reduced.</li></ul>